package com.naihe.anderson.sms.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.naihe.anderson.sms.R;
import com.naihe.anderson.sms.entity.BaseSetCheckObject;

public class BaseSetCheckAdapter extends BaseAdapter {

	Context mContext;
    List<BaseSetCheckObject> objects;
    boolean canSetOther = true;

    public BaseSetCheckAdapter( Context context, List<BaseSetCheckObject> objects, boolean canSetOther) {
        mContext = context;
        this.objects = objects;
        this.canSetOther = canSetOther;
    }
    
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return objects.size();
	}

	@Override
	public BaseSetCheckObject getItem(int position) {
		// TODO Auto-generated method stub
		return objects.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View view = null;
        if (convertView == null) {
            LayoutInflater inflater = LayoutInflater.from(mContext);
            view = inflater.inflate(R.layout.base_set_check_item, null);
        } else {
            view = convertView;
        }
        RelativeLayout checkLayout = (RelativeLayout) view.findViewById(R.id.set_sms_check_layout);
        TextView title = (TextView) view.findViewById(R.id.title);
        TextView explain = (TextView) view.findViewById(R.id.explain);
        CheckBox widgetCheckbox = (CheckBox) view.findViewById(R.id.widget_checkbox);
        BaseSetCheckObject checkObject = objects.get(position);
        
        if(canSetOther){
        	checkLayout.setBackgroundResource(R.drawable.listview_bg);
        } else {
        	if(checkObject.isHeadSet()){
        		checkLayout.setBackgroundResource(R.drawable.listview_bg);
        	} else {
        		checkLayout.setBackgroundResource(R.drawable.unavailable_line_bk);
        	}
        }
        
        title.setText(checkObject.getTitle());
        explain.setText(checkObject.getExplain());
        widgetCheckbox.setChecked(checkObject.isIfChcek());
        if(!checkObject.isHasCheck()){
        	widgetCheckbox.setVisibility(View.INVISIBLE);
        } else {
        	widgetCheckbox.setVisibility(View.VISIBLE);
        }
        
        checkObject = null;
		return view;
	}
	
	public void setCanSetOther(boolean canSetOther){
		this.canSetOther = canSetOther;
	}

	public boolean getCanSetOther(){
		return canSetOther;
	}
}
